import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Q1 {
    public static void main(String[] args) {
        int[] nums = new int[] {1,1,2,1};
        int output = minimumDistance(nums);
        System.out.println(output);

    }
    public static int minimumDistance(int[] nums) {
        // 找到所有相同元素的下标
        Map<Integer, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            ArrayList<Integer> arrayList;
            if (!map.containsKey(num)) {
                arrayList= new ArrayList<>();
            } else {
                arrayList = map.get(num);
            }
            arrayList.add(i);
            map.put(num, arrayList);
        }
        System.out.println(map);
        int ans = Integer.MAX_VALUE;
        for (ArrayList<Integer> indexList : map.values()) {
            int distance = distance(indexList);
            ans = Math.min(ans, distance);
        }
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
    public static int distance(List<Integer> nums) {
        if (nums.size() <3) {
            return Integer.MAX_VALUE;
        }
        int fast = 2;
        int slow = 0;
        int ret = Integer.MAX_VALUE;
        while (fast < nums.size()) {
            int distance = 2 * Math.abs((nums.get(fast) - nums.get(slow)));
            ret = Math.min(ret, distance);
            fast ++;
            slow ++;
        }
        return ret;
    }
}